package cn.edu.zafu.bigdata.yh;
import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        String hostname = "localhost"; // 服务器的主机名或IP地址
        int port = 6666; // 服务器的端口号
        try {
            // 创建客户端的Socket，连接服务器
            Socket socket = new Socket(hostname, port);
            System.out.println("Connected to server");

            // 使用Socket的输出流发送数据
            PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
            // 使用Socket的输入流读取数据
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // 发送消息给服务器
            output.println("Hello, server! This is client.");

            // 读取服务器的回复
            String serverMessage = input.readLine();
            System.out.println("Message from server: " + serverMessage);

            // 关闭连接
            socket.close();
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostname);
            System.exit(1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
